package cn.zymall.merchant.service.impl;

import cn.zymall.common.model.Coupon;
import cn.zymall.common.model.UserCoupon;
import cn.zymall.common.utils.time.TimeUtil;
import cn.zymall.merchant.mapper.UserCouponMapper;
import cn.zymall.merchant.service.UserCouponService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserCouponServiceImpl
 * @Description TODO
 * @Author chenmei
 * @Date 2020/8/24 21:35
 * @Version 1.0
 **/
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {

	@Autowired
	MerchantCouponServiceImpl merchantCouponService;

	@Autowired
	TimeUtil timeUtil;
   /**
    * 根据优惠卷id,获取哪些用户领过该优惠
    * @Author: chenmei
    * @param couponId
    * @Date: 2020/8/24 21:46
    * @return:
    **/
	@Override
	public List<UserCoupon> getUserCouponByCouponId(Integer couponId){
		QueryWrapper<UserCoupon> qw=new QueryWrapper<>();
		qw.eq("COUPON_ID",couponId);
		return baseMapper.selectList(qw);
	};

	/**
	 * 用户领取优惠卷，添加领取记录
	 * @Author: chenmei
	 * @param userCoupon 用户与优惠卷实体类
	 * @Date: 2020/8/25 23:24
	 * @return:
	 **/
	@Override
	public Integer addUserCoupon(UserCoupon userCoupon){
		userCoupon.setReceiveTime(timeUtil.getSystemDate());
		Integer i= baseMapper.insert(userCoupon);  //添加领取记录
		merchantCouponService.updateMerchantCouponNum(userCoupon.getCouponId());//优惠卷余数减1

		return i;
	}

	/**
	 * 获取用户领了该卷多少次
	 * @param couponId
	 * @param userId
	 * @return
	 */
	@Override
	public Integer getUserCouponCount(Integer couponId, Integer userId) {
		return baseMapper.getUserCouponCount(couponId,userId);
	}

	/**
	 * 获取用户领取过的卷
	 * @Author: chenmei
	 * @param userId 用户id
	 * @Date: 2020/8/26 14:42
	 * @return:
	 **/
	@Override
	public List<Coupon> getUserCouponByUserId(Integer userId) {
		return baseMapper.getUserCouponByUserId(userId);
	}


}
